﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace RN.ERP.Web.Handlers
{
    /// <summary>
    /// LogoutHandler 的摘要说明
    /// </summary>
    public class LogoutHandler : IHttpHandler, IRequiresSessionState
    {
        private const string DEFAULT_LOGIN_PAGE = "~/login.aspx";

        public void ProcessRequest(HttpContext context)
        {
            context.Session[SessionConstant.SESSION_USERINFO] = null;
          
            if (context.Request.Cookies[CookieConstant.COOKIE_USERGUID] != null)
            {
                context.Request.Cookies[CookieConstant.COOKIE_USERGUID].Value = string.Empty;
            }

            ToLoginPage(context);
        }

        /// <summary>
        /// 跳转到登陆页
        /// 如果有参数，则跳到参数指定的登录页，否则使用默认页
        /// </summary>
        private void ToLoginPage(HttpContext context)
        {
            string loginPageUrl = context.Request.QueryString["LoginPageUrl"];
            loginPageUrl = String.IsNullOrEmpty(loginPageUrl) ? DEFAULT_LOGIN_PAGE : loginPageUrl;
            context.Response.Redirect(loginPageUrl);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}